package com.fuxi.cloud.common.interceptor;

import com.fuxi.cloud.common.util.JimuSpringContextUtils;
import com.fuxi.cloud.config.client.JmReportTokenClient;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.minidao.aspect.EmptyInterceptor;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * MinidaoInterceptor
 *
 * @Author: 扫地僧
 * @CreateTime: 2021-04-19
 */
@Component("minidaoInterceptor")
public class MinidaoInterceptor implements EmptyInterceptor {
    private JmReportTokenClient client;

    public MinidaoInterceptor() {
    }

    public boolean onInsert(Field[] fields, Object obj) {
        HashMap var3 = new HashMap();

        for(int var4 = 0; var4 < fields.length; ++var4) {
            fields[var4].setAccessible(true);
            String var5 = fields[var4].getName();
            if (this.client == null) {
                this.client = (JmReportTokenClient) JimuSpringContextUtils.getBean(JmReportTokenClient.class);
            }

            if (this.client != null) {
                String var6 = this.client.getUsername();
                if (StringUtils.isNotEmpty(var6) && "createBy".equals(var5)) {
                    var3.put("createBy", var6);
                }
            }

            if ("createTime".equals(var5)) {
                var3.put("createTime", new Date());
            }
        }

        try {
            setFieldValue(var3, obj);
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean onUpdate(Field[] fields, Object obj) {
        HashMap var3 = new HashMap();

        for(int var4 = 0; var4 < fields.length; ++var4) {
            fields[var4].setAccessible(true);
            String var5 = fields[var4].getName();
            if (this.client == null) {
                this.client = (JmReportTokenClient)JimuSpringContextUtils.getBean(JmReportTokenClient.class);
            }

            if (this.client != null) {
                String var6 = this.client.getUsername();
                if (StringUtils.isNotEmpty(var6) && "updateBy".equals(var5)) {
                    var3.put("updateBy", var6);
                }
            }

            if ("updateTime".equals(var5)) {
                var3.put("updateTime", new Date());
            }
        }

        try {
            setFieldValue(var3, obj);
        } catch (Exception var7) {
            var7.printStackTrace();
        }

        return false;
    }

    private static void setFieldValue(Map<Object, Object> map, Object bean) throws Exception {
        Class var2 = bean.getClass();
        Method[] var3 = var2.getDeclaredMethods();
        Field[] var4 = var2.getDeclaredFields();
        Field[] var5 = var4;
        int var6 = var4.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            Field var8 = var5[var7];
            String var9 = var8.getType().getSimpleName();
            String var10 = var8.getName();
            String var11 = pareSetName(var10);
            if (checkMethod(var3, var11) && map.containsKey(var10)) {
                Object var12 = map.get(var10);
                Method var13 = var2.getMethod(var11, var8.getType());
                if (null != var12) {
                    if ("String".equals(var9)) {
                        var13.invoke(bean, (String)var12);
                    } else if ("Double".equals(var9)) {
                        var13.invoke(bean, (Double)var12);
                    } else if ("int".equals(var9)) {
                        int var14 = Integer.valueOf((String)var12);
                        var13.invoke(bean, var14);
                    } else {
                        var13.invoke(bean, var12);
                    }
                }
            }
        }

    }

    private static String pareSetName(String fldname) {
        if (null != fldname && !"".equals(fldname)) {
            String var1 = "set" + fldname.substring(0, 1).toUpperCase() + fldname.substring(1);
            return var1;
        } else {
            return null;
        }
    }

    private static boolean checkMethod(Method[] methods, String met) {
        if (null != methods) {
            Method[] var2 = methods;
            int var3 = methods.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                Method var5 = var2[var4];
                if (met.equals(var5.getName())) {
                    return true;
                }
            }
        }

        return false;
    }
}

